package algorithms.roughgarden.part2.jobsceduler;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {

	private static List<Job> jobList;
	public static void main(String[] args) throws IOException {
		InputStream is = Main.class.getResourceAsStream("jobs.txt");
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
		String str;
		str = br.readLine();
		int length = Integer.parseInt(str);
		jobList = new ArrayList<Job>(length);
		String[] strArr;
		while((str = br.readLine())!=null){
			strArr = str.split(" ");
			jobList.add(new Job(Short.parseShort(strArr[1]), Short.parseShort(strArr[0])));
		}
		/*jobList.add(new Job((short)1, (short)3));
		jobList.add(new Job((short)2, (short)2));
		jobList.add(new Job((short)3, (short)1));*/
		br.close();
		Collections.sort(jobList,Collections.reverseOrder(new JobDifferenceComparator()));
		//Collections.sort(jobList,Collections.reverseOrder(new JobRatioComparator()));
		printObjectiveFunctionValue();
	}
	private static void printObjectiveFunctionValue(){
		int time = 0;
		long functionValue = 0;
		for(Job job:jobList){
			time+=job.getLength();
			functionValue+=(job.getWeight()*time);
		}
		System.out.println(functionValue);
	}

}
